﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Win32;

using GraphSharp.Controls;
using Flux_Balance_Analysis.Graphs;

using System.Diagnostics;

using Microsoft.SolverFoundation.Common;
using Microsoft.SolverFoundation.Services;

namespace Flux_Balance_Analysis
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ConstraintsWindow mgr;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void cmdAddMetabolte_Click(object sender, RoutedEventArgs e)
        {
            MetaboliteWindow w = new MetaboliteWindow();
            bool? result = w.ShowDialog();
            if (result.HasValue && result.Value)
            {
                FBACore.AddMetabolite(w.Metabolite);
            }
        }

        private void cmdAddReaction_Click(object sender, RoutedEventArgs e)
        {
            ReactionWindow w = new ReactionWindow(null);
            bool? result = w.ShowDialog();
            if (result.HasValue && result.Value)
            {
                FBACore.AddReaction(w.Reaction);
            }
        }

        private void cmdUpdateLayout_Click(object sender, RoutedEventArgs e)
        {
            viewer.Update();
        }

        private void cmdNew_Click(object sender, RoutedEventArgs e)
        {
            FBACore.Clear();
        }

        private void cmdLoad_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            ofd.Multiselect = false;
            ofd.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory + "Saves\\";
            ofd.Filter = "Save Files | *.sav";
            if (ofd.ShowDialog() == true)
            {
                FBACore.Clear();
                FBACore.Load(ofd.FileName);
                txtObjective.Text = FBACore.Objective;
            }
        }

        private void cmdSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.AddExtension = true;
            sfd.CheckPathExists = true;
            sfd.Filter = "Save Files | *.sav";
            sfd.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory + "Saves\\";
            if (sfd.ShowDialog() == true)
            {
                FBACore.Save(sfd.FileName);
            }
        }

        SolidColorBrush GetColor(double rap)
        {
            byte r = 00;
            byte g = (byte)(255 - rap * 255);
            byte b = 255;

            return new SolidColorBrush(Color.FromRgb(r, g, b));
        }

        private void cmdSolve_Click(object sender, RoutedEventArgs e)
        {
            Dictionary<string, double> results = FBACore.Calculate();
            IEnumerable<EdgeControl> ctrls = viewer.graphLayout.Children.OfType<EdgeControl>();
            foreach (var solution in results.Keys)
            {
                ctrls.FirstOrDefault(ctrl => (ctrl.Edge as Reaction).Name == solution).Foreground = GetColor(results[solution] / 10.0);
            }
        }

        private void txtObjective_TextChanged(object sender, TextChangedEventArgs e)
        {
            FBACore.Objective = txtObjective.Text;
        }

        private void cmdConstraintsManager_Click(object sender, RoutedEventArgs e)
        {
            if (mgr == null)
            {
                mgr = new ConstraintsWindow();
                mgr.Closed += mgr_Closed;
                mgr.Show();
            }
        }

        void mgr_Closed(object sender, EventArgs e)
        {
            mgr = null;
        }
    }
}
